package com.yjt.fresh.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.willy.ratingbar.ScaleRatingBar;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiAddComment;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanUserComment;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论
 * Created by mcc on 2018/8/2.
 */

public class AtyComment extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.ivAppAtyComment)
    ImageView mIv;
    @BindView(R.id.ratingBarAppAtyComment)
    ScaleRatingBar mRatingBar;
    @BindView(R.id.etAppAtyCommentContent)
    EditText mEtContent;
    @BindView(R.id.tvAppAtyComment)
    TextView mTvSubmit;
    private BeanUserComment.BeanUserCommentInfo mCommentInfo;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_comment;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mCommentInfo = (BeanUserComment.BeanUserCommentInfo) getIntent().getSerializableExtra(MyConstants.COMMENT_BEAN);
        Glide.with(mContext).load(mCommentInfo.getSKU_IMG()).into(mIv);
    }

    @Override
    protected void initEvent() {
        initClick();
    }

    private void initClick() {
        mTvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRatingBar.getNumStars() <= 0){
                    Toast.makeText(mContext, "亲,你没有给星星哦", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mEtContent.getText().toString())){
                    Toast.makeText(mContext, "亲,请填写评论内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                submit();
            }
        });
    }

    HttpOnNextListener<BaseResultEntity> mCommentListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("评论成功");
            finish();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void submit() {
        ApiAddComment apiAddComment = new ApiAddComment(mCommentListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("GOODSORDER_ID",mCommentInfo.getORDERS_ID());
        map.put("GOODS_ID",mCommentInfo.getGOODS_ID());
        map.put("ORDERSITEM_ID",mCommentInfo.getORDERSITEM_ID());
        map.put("GOODS_NAME",mCommentInfo.getTITLE());
        map.put("USER_NAME", PreferenceManager.getInstance().getUserInfo().getNICKNAME());
        map.put("PHONE",PreferenceManager.getInstance().getUserInfo().getPHONE());
        map.put("EVALUATE_CONTENT",mEtContent.getText().toString());
        String type = "";
        int numStars = mRatingBar.getNumStars();
        if (numStars == 1){
            type = "3";
        }else if (numStars > 1 && numStars <= 3){
            type = "2";
        }else {
            type = "1";
        }
        map.put("EVALUATE_TYPE",type);
        map.put("EVALUATE_GRADE",numStars+"");

        apiAddComment.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiAddComment);
    }

}
